<?php
class Zendvn_Captcha_Image extends Zend_Captcha_Image{
	
	protected function _randomSize(){
        return true;
    }
	
	public function __construct($arrParam = null, $options = null){
		parent::__construct();
		//$captcha = new Zend_Captcha_Image();
		$width = !empty($arrParam['width'])?$arrParam['width']:300;
		$height = !empty($arrParam['height'])?$arrParam['height']:150;
		
		$wordlen 	= !empty($arrParam['wordlen'])?$arrParam['wordlen']:'4';
		$fsize 		= !empty($arrParam['fsize'])?$arrParam['fsize']:'32';
		$dot 		= !empty($arrParam['dot'])?$arrParam['dot']:'5';
		$line 		= !empty($arrParam['line'])?$arrParam['line']:'5';
		
		$this->setImgDir(PUBLIC_PATH . '/captcha/img');
		$this->setImgUrl(PUBLIC_URL . '/captcha/img' );
		$this->setWidth($width);
		$this->setHeight($height);
		$this->setFont(PUBLIC_PATH . '/captcha/font/BRITANIC.TTF');
		$this->setFontSize($fsize);
		$this->setWordlen($wordlen);
		$this->setDotNoiseLevel($dot);
		$this->setLineNoiseLevel($line);
		
		$this->generate();
		//Luu vao session duoc tao ra boi Captcha
		$namespace = 'Zend_Form_Captcha_' . $this->getId();
		$ssCaptcha = new Zend_Session_Namespace($namespace);
		$ssCaptcha->word = $this->getWord();
		//$this->view->captchaID = $captcha->getId();		
		
	}
	
	public function removeCaptchaImg(){
	
		$directory = $this->getImgDir();
		
		$arrFolder = scandir($directory);
		$captchaImg = $this->getId() . $this->getSuffix();
		$pattern = '#^(\.|\.\.|' . $captchaImg .')$#';
		foreach ($arrFolder as $val){
			if(!preg_match($pattern, $val)){
				$filename = $this->getImgDir() . '/' . $val;
				unlink($filename);
			}
		}
	}
}